﻿using System;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ProjectManager.Masters.ModuleData;
using ApplicationLibrary.SqlConnector;
using System.Collections.Generic;

namespace ProjectManager.Masters.Forms
{
    public partial class Sessions : ApplicationLibrary.DBManager.DocumentForm
    {
        private EnumsManager<Status> StatusManager = new EnumsManager<Status>(Properties.Resources.ResourceManager);
        private PartialCounter CounterManager = new PartialCounter(Properties.Resources.ResourceManager);
        private RRProjects rrProjects = null;

        public Sessions()
            : base("Sessions")
        {
            InitializeComponent();
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerSessions("Sessions", new RadarSessions(), this);
            dbManager.AddMaster(this, PM_Session.Name, true);

            cntNumber.AttachCounterType(Counters.Session.Int(), GlobalInfo.CurrentDate, null);
            CounterManager.AttachTo(cbbFatherType);

            base.OnAttachData();
        }

        protected override void OnBindData()
        {
            BindControl(cntNumber, PM_Session.ID, NullValue.NotSet, Findable.YES);
            BindControl(txtDescription, PM_Session.Description);
            BindControl(dtpStarted, PM_Session.StartDate);
            BindControl(dtpEnded, PM_Session.EndDate);
            BindControl(cbbFatherType, PM_Session.Type, "SelectedValue", NullValue.NotSet);
            BindControl(cbbFather, PM_Session.Code, "SelectedValue", NullValue.NotSet);

            BindCounter(cntNumber);
            base.OnBindData();
        }

        protected override bool OnEnableToolbarButtons(System.Windows.Forms.ToolStripButton button)
        {
            if (button.Name == "tbnNew")
                return false;

            return base.OnEnableToolbarButtons(button);
        }

        protected override void OnDisableControlsForEdit()
        {
            cntNumber.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            rrProjects = new RRProjects(CounterManager.GetValue());
            if (GlobalInfo.LoginInfo.ProviderType == ProviderType.SQLite)
                cbbFather.AttachDataReader<long>(rrProjects, PM_Project.Code, PM_Project.Description);
            else
                cbbFather.AttachDataReader<int>(rrProjects, PM_Project.Code, PM_Project.Description);
        }

        private class PartialCounter : EnumsManager<Counters>
        {
            public PartialCounter(System.Resources.ResourceManager manager)
                : base(manager)
            {}

            public override bool DisplayValue(Counters t)
            {
                return t != Counters.Session;
            }
        }
    }

    #region DbManager dbmanagerSessions

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerSessions : DBManager
    {
        public dbmanagerSessions(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PM_Session  ();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(PM_Session.ID, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", PM_Session.ID);
            sParam1.Value = PM_Project.Type.DefaultValue;
            PList.Add(sParam1);

            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion DbManager dbmanagerSessions
}